package com.lizard.schedule;

import android.app.Application;
import android.content.Context;
import android.content.res.Configuration;
import android.os.Handler;

import com.iflytek.cloud.SpeechConstant;
import com.iflytek.cloud.SpeechUtility;
import com.lizard.schedule.log.ScheduleLog;
import com.lizard.schedule.manager.AccountManager;
import com.lizard.schedule.manager.msc.SpeechRecognizerManager;
import com.lizard.schedule.manager.umeng.UmengManager;
import com.lizard.schedule.manager.wx.WxConst;
import com.lizard.schedule.net.http.request.HttpRequest;
import com.lizard.schedule.utils.CrashUtil;
import com.lizard.schedule.utils.DeviceUtil;
import com.lizard.schedule.utils.JsonUtil;
import com.lizard.schedule.utils.ScheduleUtil;

public class ScheduleApplication extends Application {

    private final String TAG = getClass().getSimpleName();

    private final String TAG_APPLICATION_LIFE = TAG + " life";

    /**
     * application生命周期log是否打印
     */
    private final boolean mApplicationLifeLogAble = true;

    public static Context applicationContext;

    public static Handler handler;

    public static final String PACKAGE_NAME = "com.lizard.schedule";

    @Override
    public void onCreate() {
        super.onCreate();

        applicationContext = getApplicationContext();

        String processAppName = ScheduleUtil.getAppProcessName(android.os.Process.myPid());
        if (processAppName == null || !(processAppName.equals(PACKAGE_NAME))) {
            return;
        }

        // 工具类的一些初始化
        initUtils();

        logLife("onCreate()");

        handler = new Handler();

        // 微信相关常量初始化
        WxConst.init();

        // 从本地缓存获取最近一次登录信息存到内存缓存
        AccountManager.getInstance().checkOutLastLoginInfoFromLocal();

        // 初始化友盟一些设置
        UmengManager.getInstance().init();

        // 语音听写SDK初始化
        SpeechRecognizerManager.getInstance().init();

    }

    /**
     * 工具类的一些初始化
     */
    private void initUtils(){
        DeviceUtil.init();
        ScheduleUtil.init();
        JsonUtil.init();
    }

    @Override
    public void onLowMemory() {
        super.onLowMemory();
        logLife("onLowMemory()");
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        logLife("onConfigurationChanged(Configuration newConfig)");
    }

    @Override
    public void onTerminate() {
        super.onTerminate();
        logLife("onTerminate()");
    }

    /**
     * 生命周期log输出
     */
    private void logLife(String life){
        if(mApplicationLifeLogAble){
            ScheduleLog.v(TAG_APPLICATION_LIFE, life);
        }
    }
}
